package com.slipper.system.codeless.service;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.slipper.constants.Constants;
import com.slipper.util.Tool;
import com.slipper.system.codeless.bo.DataModelBO;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import com.slipper.system.codeless.dao.DataTableFieldDao;
import com.slipper.system.codeless.bo.DataTableFieldBO;

import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;

@Service
@ReadTransactional
public class DataTableFieldService {
    @Resource
    private DataTableFieldDao dataTableFieldDao;
    @Resource
    private DataTableFieldStyleService dataTableFieldStyleService;
    
    public DataTableFieldBO queryBy(String id) {
        DataTableFieldBO dataTableFieldBO = dataTableFieldDao.queryBy(id);
        dataTableFieldBO.setStyleList(dataTableFieldStyleService.queryFieldId(id));
        return dataTableFieldBO;
    }
    
    public List<DataTableFieldBO> queryList(DataTableFieldBO bo) {
        return dataTableFieldDao.queryList(bo);
    }
    
    public PageInfo<DataTableFieldBO> queryPage(DataTableFieldBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(dataTableFieldDao.queryList(bo));
    }
    
    @WriteTransactional
    public int save(String tableId, List<DataTableFieldBO> list, String employeeId) {
        if (Tool.isBlank(tableId)) {
            return 0;
        }

        // 保存/更新数据
        int i = 0;
        for (DataTableFieldBO field : list) {
            field.setTableId(tableId);
            field.setOrdinal(++i);
            if (Tool.isBlank(field.getId())) {
                field.setId(UUID.getUUID());
                field.setUpdateId(employeeId);
                field.setCreateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                field.setCreateTime(System.currentTimeMillis());
                dataTableFieldDao.insert(field);
            } else {
                field.setUpdateId(employeeId);
                field.setUpdateTime(System.currentTimeMillis());
                dataTableFieldDao.update(field);
            }
            dataTableFieldStyleService.save(field.getId(), field.getStyleList(), employeeId);
        }

        // 删除被删除的数据
        DataTableFieldBO qo = new DataTableFieldBO();
        qo.setTableId(tableId);
        dataTableFieldDao.queryList(qo).stream()
                .filter(field -> list.stream().noneMatch(temp -> field.getId().equals(temp.getId())))
                .forEach(this::delete);
        return 1;
    }

    @WriteTransactional
    public void saveList(List<DataTableFieldBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (dataTableFieldDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                dataTableFieldDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                dataTableFieldDao.insert(item);
            }
        });
    }
    
    @WriteTransactional
    public int delete(DataTableFieldBO bo) {
        dataTableFieldStyleService.deleteByFieldId(bo.getId());
        return dataTableFieldDao.delete(bo.getId());
    }

    @WriteTransactional
    public void createDefault(String tableId, DataModelBO bo) {
        bo.getFieldList().forEach(field -> {
            if (Constants.YES.equals(field.getBuiltin())
                    && !field.getFieldName().equalsIgnoreCase("update_time")
                    && !field.getFieldName().equalsIgnoreCase("create_time")) {
                return;
            }

            DataTableFieldBO fieldBO = new DataTableFieldBO();
            fieldBO.setId(UUID.getUUID());
            fieldBO.setTableId(tableId);
            fieldBO.setFieldId(field.getId());
            fieldBO.setFieldName(field.getFieldName());
            fieldBO.setAliasName(field.getShowName());
            fieldBO.setWidth(
                    field.getFieldName().equalsIgnoreCase("update_time")
                        || field.getFieldName().equalsIgnoreCase("create_time") ? 165 : -120);
            fieldBO.setIfMerge(Constants.NO);
            fieldBO.setWrap(Constants.NO);
            fieldBO.setHeaderAlign("居中");
            fieldBO.setContentAlign("居左");
            fieldBO.setOrdinal(field.getOrdinal());
            fieldBO.setUpdateId(bo.getUpdateId());
            fieldBO.setCreateId(bo.getCreateId());
            fieldBO.setUpdateTime(System.currentTimeMillis());
            fieldBO.setCreateTime(System.currentTimeMillis());
            dataTableFieldDao.insert(fieldBO);
        });
    }

    @WriteTransactional
    public void deleteByTableId(String tableId) {
        if (Tool.isBlank(tableId)) {
            return;
        }

        queryByTableId(tableId).forEach(this::delete);
    }

    @WriteTransactional
    public void copyByTableId(String oldTableId, String newTableId, String employeeId) {
        if (Tool.isBlank(oldTableId) || Tool.isBlank(newTableId)) {
            return;
        }

        DataTableFieldBO qo = new DataTableFieldBO();
        qo.setTableId(oldTableId);
        dataTableFieldDao.queryList(qo).forEach(field -> {
            String oldField = field.getId();
                    
            field.setId(UUID.getUUID());
            field.setTableId(newTableId);
            field.setUpdateId(employeeId);
            field.setCreateId(employeeId);
            field.setUpdateTime(System.currentTimeMillis());
            field.setCreateTime(System.currentTimeMillis());
            dataTableFieldDao.insert(field);

            dataTableFieldStyleService.copyByFieldId(oldField, field.getId(), employeeId);
        });
    }

    public List<DataTableFieldBO> queryByTableId(String tableId) {
        if (Tool.isBlank(tableId)) {
            return Collections.emptyList();
        }

        DataTableFieldBO qo = new DataTableFieldBO();
        qo.setTableId(tableId);
        List<DataTableFieldBO> fieldList = dataTableFieldDao.queryList(qo);
        fieldList.forEach(field -> field.setStyleList(dataTableFieldStyleService.queryFieldId(field.getId())));
        return fieldList;
    }
}
